package cn.gp.chatclient.ui.panel;

import cn.gp.chatclient.model.UserVo;
import cn.gp.chatclient.ui.component.NavigationButton;
import cn.gp.chatclient.utils.ImageUtil;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
/**
 * @ClassName LeftPanel
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 9:10
 * @Version JDK17
 */
@Slf4j
public class LeftPanel extends JPanel {
    public interface NavigationListener {
        void onNavigationChanged(String panelName);
    }

    private NavigationListener listener;
    private JLabel avatarLabel;
    private NavigationButton currentSelectedButton;

    public LeftPanel(UserVo currentUser) {
        setPreferredSize(new Dimension(80, 0));
        setBackground(new Color(40, 44, 52));
        setLayout(new BorderLayout());

        initComponents();
    }

    private void initComponents() {
        // 顶部头像区域
        JPanel topPanel = new JPanel();
        topPanel.setBackground(new Color(40, 44, 52));
        topPanel.setPreferredSize(new Dimension(80, 120));
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 20));

        avatarLabel = new JLabel();
        avatarLabel.setPreferredSize(new Dimension(50, 50));
        topPanel.add(avatarLabel);

        // 导航按钮区域
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(new Color(40, 44, 52));
        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));

        // 创建导航按钮
        NavigationButton contactsBtn = createNavigationButton("联系人", "icons/contacts.png", "contacts");
        NavigationButton chatBtn = createNavigationButton("聊天", "icons/chat.png", "chat");
        NavigationButton profileBtn = createNavigationButton("我的", "icons/my.png", "profile");
        NavigationButton friendRequestBtn = createNavigationButton("新的朋友", "icons/newfriend.png", "friendRequests");

        // 设置按钮的最大宽度
        Dimension maxSize = new Dimension(80, 40);
        contactsBtn.setMaximumSize(maxSize);
        chatBtn.setMaximumSize(maxSize);
        profileBtn.setMaximumSize(maxSize);
        friendRequestBtn.setMaximumSize(maxSize);

        // 添加按钮和间距
        centerPanel.add(Box.createVerticalStrut(10));
        centerPanel.add(contactsBtn);
        centerPanel.add(Box.createVerticalStrut(15));
        centerPanel.add(chatBtn);
        centerPanel.add(Box.createVerticalStrut(15));
        centerPanel.add(profileBtn);
        centerPanel.add(Box.createVerticalStrut(15));
        centerPanel.add(friendRequestBtn);
        centerPanel.add(Box.createVerticalStrut(10));

        // 底部空白区域
        JPanel bottomPanel = new JPanel();
        bottomPanel.setBackground(new Color(40, 44, 52));
        bottomPanel.setPreferredSize(new Dimension(80, 100));

        add(topPanel, BorderLayout.NORTH);
        add(centerPanel, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.SOUTH);

        // 默认选中联系人按钮
        setSelectedButton(contactsBtn);
    }

    private NavigationButton createNavigationButton(String text, String iconPath, String panelName) {
        NavigationButton button = new NavigationButton(text, iconPath);
        button.setAlignmentX(Component.CENTER_ALIGNMENT); // 居中对齐
        button.addActionListener(e -> {
            if (listener != null) {
                // 检查是否是个人信息面板，如果是，暂时不刷新数据
                if ("profile".equals(panelName)) {
                    // 暂时不进行任何检查，直接切换面板
                    log.info("切换到个人信息面板");
                }
                listener.onNavigationChanged(panelName);
                setSelectedButton(button);
            }
        });
        return button;
    }

    private void setSelectedButton(NavigationButton button) {
        if (currentSelectedButton != null) {
            currentSelectedButton.setSelected(false);
        }
        currentSelectedButton = button;
        currentSelectedButton.setSelected(true);
    }

    public void setNavigationListener(NavigationListener listener) {
        this.listener = listener;
    }

    public void setAvatar(String avatarPath) {
        ImageIcon icon = ImageUtil.getAvatarIcon(avatarPath, 50, 50);
        avatarLabel.setIcon(icon);
    }
}
